import { reqregister } from "../../http/api";
import { Toast } from "antd-mobile";
//state
const initState = {
  //初始化user
  user: {
    phone: "",
    nickname: "",
    password: "",
  },
};

//action type
const TYPES = {
  //修改user的type
  REGISTER_CHANGE_USER: "REGISTER_CHANGE_USER",
};

//reducer
const reducer = (state = initState, action) => {
  switch (action.type) {
    //修改user的reducer
    case TYPES.REGISTER_CHANGE_USER:
      return {
        ...state,
        user: {
          ...state.user,
          [action.key]: action.value,
        },
      };
    default:
      return state;
  }
};

//action creator
export const actions = {
  //修改user的cation
  changeUser: (key, value) => ({
    type: TYPES.REGISTER_CHANGE_USER,
    key,
    value,
  }),
  //注册
  doRegister: (push) => {
    return (dispatch, getState) => {
      let user = getUser(getState());
      reqregister(user).then((res) => {
        if (res.data.code == 200) {
          Toast.show({
            content: res.data.msg,
           
          });
          push("/login")
        }
      });
    };
  },
};

//selector导出数据给组件
//导出user
export const getUser = (state) => state.register.user;

//导出reducer
export default reducer;
